package com.special.threademo.concurrency.executorsDemo;

import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.stream.IntStream;

/**
 * Created with IDEA2020.3
 * author:LiuYC
 * DateTime:2021/1/20  16:05
 * 按照自定义的形式创建，达到更灵活和想要的抛出的形式进行demo
 * AbortPolicy 对于超过创建的和已经多的线程的处理方法的解决方案
 * discardpolicy也是另外的一种处理方法、
 *  自己定义线程池的方法：拒绝策略的定制化和个性化
 */
public class CustomDemo {
    public static void main(String[] args) {
        ThreadPoolExecutor executorService = new ThreadPoolExecutor(3, 5, 0, TimeUnit.SECONDS, new LinkedBlockingQueue<>(3), new ThreadPoolExecutor.DiscardOldestPolicy());
//        ThreadPoolExecutor executorService = new ThreadPoolExecutor(3, 5, 0, TimeUnit.SECONDS, new LinkedBlockingQueue<>(3), new ThreadPoolExecutor.DiscardPolicy());
//        ThreadPoolExecutor executorService = new ThreadPoolExecutor(3, 5, 0, TimeUnit.SECONDS, new LinkedBlockingQueue<>(3), new ThreadPoolExecutor.CallerRunsPolicy());
//        ThreadPoolExecutor executorService = new ThreadPoolExecutor(3, 5, 0, TimeUnit.SECONDS, new LinkedBlockingQueue<>(3), new ThreadPoolExecutor.AbortPolicy());
        IntStream.range(0, 9).forEach(i -> {
            executorService.submit(() -> {
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                IntStream.range(0, 1).forEach(j -> {
                    System.out.println(Thread.currentThread().getName());
                });
            });

        });
        executorService.shutdown();
    }
}